﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MetadataBasedConfig.EntityModel.StaticConstraint;
using MetadataBasedConfig.EntityModel;
using MetadataBasedConfig.EntityModel.Entity;
using System.Data.Objects;

namespace MetadataBasedConfig.Web
{
    public partial class Test : System.Web.UI.Page, ITest
    {
        #region Page Load

        protected void Page_Load(object sender, EventArgs e)
        {
            InitEnvironment();
            TestPageModel();
        }

        #endregion

        #region Test Methods

        protected void InitEnvironment()
        {
            StaticConstraintHelper.SetStaticConstraintPropertyValue("TenantID", 1);
        }

        protected void TestPageModel()
        {
            using(MDAEntities entities = Util.Create())
            {
                try
                {
                    PageRepository repository = new PageRepository() { entities = entities }; 
                    var pageList = repository.Find(ps => (from p in ps select p));
                    //var pages = (from p in entities.Pages.LoadConfigInfoDatas() select p);
                    //var ddd = pages.AsQueryable<EntityModel.Page>();
                    //pages = (from p in entities.Pages where p.TenantID==1 select p); 
                    //pages.LoadConfigInfoDatas();
                }
                catch (Exception e)
                { }
            }
        }
        #endregion
    }

    public interface ITest
    { 
    }
    public interface IRepository<T>
    {
        int Add(T model);
        int Update(T model);
        int Delete(T model);
        IList<T> Find(Func<IEnumerable<T>, IEnumerable<T>> querableFunc);
        IList<T> FindAll();
    }

    public class PageRepository : IRepository<EntityModel.Page>
    {
        public MDAEntities entities;

        public int Add(EntityModel.Page model)
        {
            throw new NotImplementedException();
        }

        public int Update(EntityModel.Page model)
        {
            throw new NotImplementedException();
        }

        public int Delete(EntityModel.Page model)
        {
            throw new NotImplementedException();
        }

        public IList<EntityModel.Page> Find(Func<IEnumerable<EntityModel.Page>, IEnumerable<EntityModel.Page>> querableFunc)
        {
            return querableFunc(entities.Pages).ToList();
        }
         
        public IList<EntityModel.Page> FindAll()
        {
            throw new NotImplementedException();
        }
    }
}